#include"Save.h"


void Save::Saving(vector<Person*>Ku, vector<Dot*>DotKu, vector<Line*>LineKu) {
	vector<User>PreUser;
	vector<Admin>PreAdmin;
	vector<OrdinaryDot>PreSaveOrdinaryDot;          
	vector<Scenery>PreSaveScenery;
	vector<Crossroad>PreSaveCrossroad;
	vector<StraightSave>PreSaveStraight;
	vector<BusStation>PreSaveBusStation;
	//曲线扩展功能补充后在此添加内容
	int PreDotID;
	for (int i = 0; i < Ku.size(); i++) {
		string a = Ku[i]->GetID();
		string b = Ku[i]->Getname();
		string c = Ku[i]->Getpassword();
		if (Ku[i]->type == 1) {
			User* p = new User(a, b, c);
			PreUser.push_back(*p);
		}
		else if (Ku[i]->type == 2) {
			Admin* p = new Admin(a, b, c);
			PreAdmin.push_back(*p);
		}
	}
	for (int i = 0; i < DotKu.size(); i++) {
		int aa = DotKu[i]->GetID();
		double bb = DotKu[i]->Getx();
		double cc = DotKu[i]->Gety();
		if (DotKu[i]->Gettype() == 1) {
			OrdinaryDot* p = new OrdinaryDot(aa, bb, cc);
			PreSaveOrdinaryDot.emplace_back(*p);
		}
		else if (DotKu[i]->Gettype() == 2) {
			string d = DotKu[i]->Getname();
			string por = DotKu[i]->GetImformation(); 
			Scenery* p = new Scenery(aa, bb, cc,d);
			p->RevisePortrait(por);
			p->SetPlace(DotKu[i]->Getplace());
			p->SetImage(DotKu[i]->GetIMAGE());
			PreSaveScenery.emplace_back(*p);
		}
		else if (DotKu[i]->Gettype() == 3) {
			vector<Period> PreGreen1 = DotKu[i]->GetGreen1();
			vector<Period> PreGreen2 = DotKu[i]->GetGreen2();
			vector<Period> PreGreen3 = DotKu[i]->GetGreen3();
			vector<Period> PreGreen4 = DotKu[i]->GetGreen4();
			Crossroad* p = new Crossroad(aa, bb, cc);
			p->SetPassingTime(PreGreen1, PreGreen2, PreGreen3, PreGreen4);
			PreSaveCrossroad.emplace_back(*p);
		}
		else if (DotKu[i]->Gettype() == 4) {
			BusStation * p = new BusStation(aa, bb, cc);
			PreSaveBusStation.emplace_back(*p);
		}
	for (int i = 0; i < LineKu.size(); i++) {
	}
		int aaa = LineKu[i]->GetEdge1ID();
		int bbb = LineKu[i]->GetEdge2ID();
		string dd = LineKu[i]->Getname();
		if (LineKu[i]->Gettype() == 1) {
			StraightSave* p = new StraightSave(aaa, bbb,dd);
			PreSaveStraight.push_back(*p);
		}
		//曲线扩展功能补充后在此添加内容
	}
	this->SaveUser=PreUser;
	this->SaveAdmin=PreAdmin;
	this->SaveOrdinaryDot=PreSaveOrdinaryDot;          //用二元组的形式同时存储对应的点和它所连的射线
	this->SaveScenery=PreSaveScenery;
	this->SaveCrossroad=PreSaveCrossroad;
	this->SaveStraight=PreSaveStraight;
	//曲线扩展功能补充后在此添加内容
	this->SaveDotID=DotID;
	this->SaveBusStation = PreSaveBusStation;
	this->SaveBusKu = BusKu;
	return;
}

void Read(Save a) {          //读取数据
	/*需要读取数据：1.vector<Person*>Ku 角色登陆信息库
	* 2.map<string, Person*>Looking_forward;  通过ID查询角色
	* 3.vector<Dot*>DotKu;
      4.vector<Line*>LineKu;
      5.map<string, Scenery>SceneryKu;        //用于查询景点信息
      6.map<Dot*, int>DotSearch;           //查询点在库中的下标，以便于最短路径算法的实现
	 */
	vector<Person*>ReadKu;
	map<string, Person*>ReadLooking_forward;
    vector<Dot*>ReadDotKu;
	vector<Line*>ReadLineKu;
	map<string, Scenery>ReadSceneryKu;        //用于查询景点信息
	map<Dot*, int>ReadDotSearch;           //查询点在库中的下标，以便于最短路径算法的实现
	for (int i = 0; i < a.SaveUser.size(); i++) {
		ReadKu.push_back(&a.SaveUser[i]);
	}
	for (int i = 0; i < a.SaveAdmin.size(); i++) {
		ReadKu.push_back(&a.SaveAdmin[i]);
	}
	Ku = ReadKu;   //更新角色库
	for (int i = 0; i < Ku.size(); i++) {
		ReadLooking_forward[Ku[i]->GetID()] = Ku[i];
	}
	Looking_forward = ReadLooking_forward;  //更新角色ID与角色的映射
	for (int i = 0; i < a.SaveOrdinaryDot.size(); i++) {
		ReadDotKu.push_back(&(a.SaveOrdinaryDot[i]));
	}
	for (int i = 0; i < a.SaveScenery.size(); i++) {
		ReadDotKu.push_back(&(a.SaveScenery[i]));
		ReadSceneryKu[a.SaveScenery[i].Getname()] = a.SaveScenery[i];
	}
	ReadSceneryKu = SceneryKu;   //更新景点名称到景点的映射库
	for (int i = 0; i < a.SaveCrossroad.size(); i++) {
		ReadDotKu.push_back(&(a.SaveCrossroad[i]));
	}
	DotKu = ReadDotKu;    //更新点库
	for (int i = 0; i < DotKu.size(); i++) {
		ReadDotSearch[DotKu[i]] = i;
	}
	DotSearch = ReadDotSearch;   //更新点下标存储映射
	for (int i = 0; i < a.SaveStraight.size(); i++) {
		int p = 0;
		for (; p < DotKu.size(); p++) {
			if (DotKu[p]->GetID() == a.SaveStraight[i].Edge1ID) {
				break;
			}
		}
		if (p == DotKu.size()) {
			throw"Error:System Failure";
		}
		int q = 0;
		for (; q < DotKu.size(); q++) {
			if (DotKu[q]->GetID() == a.SaveStraight[i].Edge2ID) {
				break;
			}
		}
		if (q == DotKu.size()) {
			throw"Error:System Failure";
		}               //找到存储线类对应的两个点
		Straight* Str = new Straight(DotKu[p], DotKu[q], a.SaveStraight[i].name);   //通过线的构造函数给点建立起连接（即建立邻接表射线结构）
		Line* L = Str;   //隐含转换，转换到基类的指针
		ReadLineKu.push_back(L);
	}
	//曲线扩展功能补充后在此添加内容
	LineKu = ReadLineKu;  //更新线库
	DotID = a.SaveDotID;   //更新最新点的ID
	BusKu = a.SaveBusKu;    //更新校车库
	return;
}

void SaveProgram(string outfile1, string outfile2, string outfile3, string outfile4, string outfile5, string outfile6, string outfile7) {     //退出程序时应执行这个函数，输出流out打开方式应为打开时去掉文件内原有内容
	Save a;
	a.Saving(Ku, DotKu, LineKu);
	ofstream out1(outfile1, ofstream::out || ofstream::trunc); //保存User信息  ID name password
	for (int i = 0; i < a.SaveUser.size(); i++) {
		out1 << a.SaveUser[i].GetID() << " " << a.SaveUser[i].Getname() << " " << a.SaveUser[i].Getpassword() << " " << endl;
	}
	out1.close();

	ofstream out2(outfile2, ofstream::out || ofstream::trunc); //保存Admin信息  ID name password
	for (int i = 0; i < a.SaveAdmin.size(); i++) {
		out2 << a.SaveAdmin[i].GetID() << " " << a.SaveAdmin[i].Getname() << " " << a.SaveAdmin[i].Getpassword() << " " << endl;
	}
	out2.close();

	ofstream out3(outfile3, ofstream::out || ofstream::trunc); //保存OrdinaryDot信息  ID x y
	for (int i = 0; i < a.SaveOrdinaryDot.size(); i++) {
		out3 << a.SaveOrdinaryDot[i].GetID() << " " << a.SaveOrdinaryDot[i].Getx() << " " << a.SaveOrdinaryDot[i].Gety() << " " << endl;
	}
	out3.close();

	ofstream out4(outfile4, ofstream::out || ofstream::trunc); //保存Scenery信息  ID x y name portrait
	for (int i = 0; i < a.SaveScenery.size(); i++) {
		out4 << a.SaveScenery[i].GetID() << " " << a.SaveScenery[i].Getx() << " " << a.SaveScenery[i].Gety() << " " << a.SaveScenery[i].Getname() << " " << a.SaveScenery[i].GetImformation() << " " << endl;
	}
	out4.close();

	ofstream out5(outfile5, ofstream::out || ofstream::trunc); //保存Crossroad信息  ID x y
	for (int i = 0; i < a.SaveCrossroad.size(); i++) {
		out5 << a.SaveCrossroad[i].GetID() << " " << a.SaveCrossroad[i].Getx() << " " << a.SaveCrossroad[i].Gety() << " " << endl;
	}
	out5.close();

	ofstream out6(outfile6, ofstream::out || ofstream::trunc); //保存BusStation信息  ID x y
	for (int i = 0; i < a.SaveBusStation.size(); i++) {
		out6 << a.SaveBusStation[i].GetID() << " " << a.SaveBusStation[i].Getx() << " " << a.SaveBusStation[i].Gety() << " " << endl;
	}
	out6.close();

	ofstream out7(outfile7, ofstream::out || ofstream::trunc); //保存Straight信息  端点1ID 端点2ID name
	for (int i = 0; i < a.SaveStraight.size(); i++) {
		out7 << a.SaveStraight[i].GetEdge1ID() << " " << a.SaveStraight[i].GetEdge2ID() << " " << a.SaveStraight[i].Getname() << " " << endl;
	}
	out7.close();

	return;
}

void ReadProgram(string infile1, string infile2, string infile3, string infile4, string infile5, string infile6, string infile7) {      //main函数开始时（即进入程序时）应执行这个函数
	Save a;
	string line;

	ifstream in1(infile1);//读取user信息，用来构造User类并存入SaveUser
	while (getline(in1, line)) {
		stringstream ss(line);
		string userID, userName, userPassword;
		ss >> userID;
		ss >> userName;
		ss >> userPassword;
		User u(userID, userName, userPassword);
		a.SaveUser.push_back(u);
	}
	in1.close();

	ifstream in2(infile2);//读取Admin信息，用来构造Admin类并存入SaveAdmin
	while (getline(in2, line)) {
		stringstream ss(line);
		string AdminID, AdminName, AdminPassword;
		ss >> AdminID;
		ss >> AdminName;
		ss >> AdminPassword;
		Admin u(AdminID, AdminName, AdminPassword);
		a.SaveAdmin.push_back(u);
	}
	in2.close();

	ifstream in3(infile3);//读取OrdinaryDot信息，用来构造OrdinaryDot类并存入SaveOrdinaryDot
	while (getline(in3, line)) {
		stringstream ss(line);
		int ID = 0; double x = 0; double y = 0;
		ss >> ID;
		ss >> x;
		ss >> y;
		OrdinaryDot odot(ID, x, y);
		a.SaveOrdinaryDot.push_back(odot);
	}
	in3.close();

	ifstream in4(infile4);//读取Scenery信息，用来构造Scenery类并存入SaveScenery
	while (getline(in4, line)) {
		stringstream ss(line);
		int ID = 0; double x = 0; double y = 0;
		string Name, Portrait;
		ss >> ID;
		ss >> x;
		ss >> y;
		ss >> Name;
		ss >> Portrait;
		Scenery sc(ID, x, y, Name);
		Set_portrait(sc, Portrait);
		a.SaveScenery.push_back(sc);
	}
	in4.close();

	ifstream in5(infile5);//读取Crossroad信息，用来构造Crossroad类并存入SaveCrossroad
	while (getline(in5, line)) {
		stringstream ss(line);
		int ID = 0; double x = 0; double y = 0;
		ss >> ID;
		ss >> x;
		ss >> y;
		Crossroad cro(ID, x, y);
		a.SaveCrossroad.push_back(cro);
	}
	in5.close();

	ifstream in6(infile6);//读取BusStation信息，用来构造BusStation类并存入SaveBusStation
	while (getline(in6, line)) {
		stringstream ss(line);
		int ID = 0; double x = 0; double y = 0;
		ss >> ID;
		ss >> x;
		ss >> y;
		BusStation bus(ID, x, y);
		a.SaveBusStation.push_back(bus);
	}
	in6.close();

	ifstream in7(infile7);//读取StraightSave信息，用来构造StraightSave类并存入SaveStraight
	while (getline(in7, line)) {
		stringstream ss(line);
		int edge1ID, edge2ID;
		string name;
		ss >> edge1ID;
		ss >> edge2ID;
		ss >> name;
		StraightSave str(edge1ID, edge2ID, name);
		a.SaveStraight.push_back(str);
	}
	in7.close();

	Read(a);
	return;
}

